<?php if (empty($this->data)) : // ถ้าไม่มีข้อมูล ?>
    <?php echo $this->element('dataNotFound'); ?>
<? else : // ถ้ามีข้อมูล ?>
<table class="data">
    <thead>
        <tr>
            <th class="no">No.</th>
            <th width="185px">Stock code <?php if ($display_stock_in_no == true) echo '/ <br /> Stock in no.'; ?></th>
            <th width="140px">Vendor Part no. <?php if ($display_stock_in_no == true) echo '/ <br /> Stock in date'; ?></th>
            <th>Vendor <?php if ($display_stock_in_no == true) echo '/ <br /> Product Description'; ?></th>
            <th width="50px">Freq.</th>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <th width="90px">Price</th>
                <?php endif; ?>
            <th width="50px">Qty.</th>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <th width="90px">Amount</th>
                <?php endif; ?>
        </tr>
    </thead>
    <tbody>
            <?php
            $total_freq = 0;
            $total_qty = 0;
            $total_amount = 0;
            foreach($this->data as $i => $data): ?>
        <tr class="background2">
            <td class="center"><?php echo $i + 1; ?></td>
            <td class="<?php echo $display_stock_in_no == true? 'bold': ''; ?>">
                        <?php echo $html->link($data['CustProductSite']['stock_code'],
                        '/cust_product_sites/view/' . $data['CustProductSite']['id'] . '/' . strtotime('now'),
                        array('target' => '_blank')); ?>
            </td>
            <td class="<?php echo $display_stock_in_no == true? 'bold': ''; ?>"><?php echo $data['CustStockInHead'][0]['dispatch_from'] == ITS_PREFIX? $data['Product']['part_no']: $data['Product']['supplier_part_no']; ?></td>
            <td class="<?php echo $display_stock_in_no == true? 'bold': ''; ?>">
                        <?php echo $data['CustStockInHead'][0]['dispatch_from'] == ITS_PREFIX?
                        $this->requestAction('system_preferences/get/company_name'): $data['Supplier']['name']; ?>
            </td>
            <td class="text-right <?php echo $display_stock_in_no == true? 'bold': ''; ?>"><?php echo $data[0]['frequency']; ?></td>
                    <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <td class="text-right <?php echo $display_stock_in_no == true? 'bold': ''; ?>"><?php echo number_format($data['CustStockIn']['price'], 2); ?></td>
                    <?php endif; ?>
            <td class="text-right <?php echo $display_stock_in_no == true? 'bold': ''; ?>"><?php echo $data[0]['stock_in_qty']; ?></td>
                    <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <td class="text-right <?php echo $display_stock_in_no == true? 'bold': ''; ?>"><?php echo number_format($data['CustStockIn']['price'] * $data[0]['stock_in_qty'], 2); ?></td>
                    <?php endif; ?>
                    <?php
                    $total_freq += $data[0]['frequency'];
                    $total_qty += $data[0]['stock_in_qty'];
                    $total_amount += $data['CustStockIn']['price'] * $data[0]['stock_in_qty'];
                    ?>
        </tr>
                <?php if ($display_stock_in_no == true) :
                    // ตัวแปรสำหรับลำดับเลข stock in no
                    $k = 1;
                    foreach($data['CustStockInHead'] as $sto) :
                        ?>
        <tr class="background3">
            <td>&nbsp;</td>
                            <?php if ($display_stock_in_no == true) : ?>
            <td class="text-right" style="padding-right: 30px">
                <span><?php echo $k; ?>.</span>&nbsp;
                <span>
                                        <?php echo $html->link($sto['stock_in_no'],
                                        '/cust_stocks_in/view/' . $sto['id'] . '/' . strtotime('now'),
                                        array('target' => '_blank')); ?>
                </span>
            </td>

            <td>&nbsp;<?php echo $sto['CustStockIn']['stock_in_date']; ?></td>
            <td><?php echo $k == 1? $data[0]['text']: '&nbsp;'; ?></td>
            <td>&nbsp;</td>
                                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <td>&nbsp;</td>
                                <?php endif; ?>
            <td class="text-right"><?php echo $sto['CustStockIn']['stock_in_qty']; ?></td>
                                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <td class="text-right"><?php echo number_format($sto['CustStockIn']['stock_in_qty'] * $data['CustStockIn']['price'], 2); ?></td>
                                <?php endif; ?>
                            <?php endif; ?>
        </tr>
                        <?php ++$k;
                    endforeach; ?>
                <?php endif; ?>
            <?php endforeach; ?>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="4" class="bold text-right">Grand Total</td>
            <td class="text-right bold"><?php echo number_format($total_freq); ?></td>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <td class="text-right">-</td>
                <?php endif; ?>
            <td class="text-right bold"><?php echo number_format($total_qty); ?></td>
                <?php if ($session->read('Auth.User.hide_price') == false) : ?>
            <td class="text-right bold"><?php echo number_format($total_amount, 2); ?></td>
                <?php endif; ?>
        </tr>
    </tfoot>
</table>
<? endif; ?>
<?php //debug($this->data); ?>